//
//  HomeTableViewController.swift
//  SwiftWeiBo
//
//  Created by wyw on 16/9/16.
//  Copyright © 2016年 Style_Y. All rights reserved.
//

import UIKit

let HomeReuseIdentifier = "HomeReuseIdentifier"

class HomeTableViewController: BaseTableViewController {

    // 保存模型数组
    fileprivate var statuses : [Status]? {
        didSet { // set方法
            // 赋值后刷新数据
            print("刷新表格")
            tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !login {
            visitorView?.setupVisitorInfo(isHome: true, imageName: "visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        // 1.设置导航栏
        setupNav()
        
        // 2.添加通知
        NotificationCenter.default.addObserver(self, selector: #selector(HomeTableViewController.change), name: NSNotification.Name(rawValue: YNotePopoverAnimatorWillShow), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(HomeTableViewController.change), name: NSNotification.Name(rawValue: YNotePopoverAnimatorWillDissmiss), object: nil)
        
        // 3.注册cell
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: HomeReuseIdentifier)
        
        // 4.加载微博数据
        loadData()
        
    }
    
    deinit {
        // 移除通知
        NotificationCenter.default.removeObserver(self)
    }
    
    // 获取微博数据
    private func loadData() {
      Status.loadStatuses { (models, error) in
        self.statuses = models;
        
        }
    }
    
    func change() {
        let titleBtn = navigationItem.titleView as! titleButton
        print(titleBtn.isSelected)
        titleBtn.isSelected = !titleBtn.isSelected
    }
    
    // MARK: - 设置导航栏
    fileprivate func setupNav() {
        // 1.设置导航栏按钮 
        navigationItem.leftBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "navigationbar_friendattention", target: self, action: #selector(HomeTableViewController.leftItemClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(HomeTableViewController.rightItemClick))
        
        // 2.设置导航栏标题
        let titleBtn = titleButton()
        titleBtn.setTitle("小天地 ", for: UIControlState.normal)
        titleBtn.addTarget(self, action: #selector(HomeTableViewController.titleBtnClick(btn:)), for: UIControlEvents.touchUpInside)
        navigationItem.titleView = titleBtn
    }
    
    func titleBtnClick(btn:titleButton) {
        
        // 2.加载控制器
        let sb = UIStoryboard(name: "Popover", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        //  2.1 设置转场代理
        vc?.transitioningDelegate = popoverAnimator
        //  2.2 设置转场样式
        vc?.modalPresentationStyle = UIModalPresentationStyle.custom
        
        present(vc!, animated: true, completion: nil)
        
    }
    func leftItemClick() {
        print(#function)
    }
    func rightItemClick() {
        let sb = UIStoryboard(name: "QRCodeViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        present(vc!, animated: true, completion: nil)
    }
    
    // MARK: - 懒加载
    // 一定要创建一个自定义转场类，强引用
    private lazy var popoverAnimator:PopoverAnimator = {
        let po = PopoverAnimator()
        po.presentFrame = CGRect(x: 100, y: 56, width: 200, height: 300)
        return po
    }()
}

extension HomeTableViewController
{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("行数")
//        return statuses?.count ?? 0
        return 20
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 1.获取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: HomeReuseIdentifier, for: indexPath)
//        // 2.设置数据
//        let status = statuses![(indexPath as NSIndexPath).row]
//        cell.textLabel?.text = status.text
        cell.contentView.backgroundColor = UIColor.green
        // 3.返回cell
        return cell
    }
}








